{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "33b987c5",
   "metadata": {},
   "source": [
    "# 基于LangChain的RAG民法典问答机器人项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d8bc94",
   "metadata": {},
   "source": [
    "## 一、索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5033e91b",
   "metadata": {},
   "source": [
    "### （1）文档加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "15f68873",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import WebBaseLoader\n",
    "import bs4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1a77e840",
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = WebBaseLoader(\n",
    "    web_path=\"https://www.court.gov.cn/zixun/xiangqing/233181.html\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fe4fc6e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = loader.load()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08d816ba",
   "metadata": {},
   "source": [
    "### （2）文本分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1f803aa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_text_splitters import RecursiveCharacterTextSplitter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ed9d3a1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_splitter = RecursiveCharacterTextSplitter(\n",
    "    chunk_size=500,\n",
    "    chunk_overlap=50\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ec926819",
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = text_splitter.split_documents(docs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87c7d5dd",
   "metadata": {},
   "source": [
    "### （3）嵌入模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3f4d2025",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.embeddings import DashScopeEmbeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d9081168",
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings_model = DashScopeEmbeddings(\n",
    "    model=\"text-embedding-v4\",\n",
    "    dashscope_api_key=\"sk-da90821cf9174fbeb854011015c67aad\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80f54ec1",
   "metadata": {},
   "source": [
    "### （4）向量数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f6ab5c2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_chroma import Chroma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "04b81ae6",
   "metadata": {},
   "outputs": [],
   "source": [
    "vector_store = Chroma(\n",
    "    embedding_function=embeddings_model,\n",
    "    persist_directory=\"./chroma_langchain_db\",\n",
    "    collection_name=\"example_collection\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "419a9b46",
   "metadata": {},
   "source": [
    "### （5）构建索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "63b873eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['4fc2372b-6753-4e50-a966-83e45d54aa93',\n",
       " '7a6885e4-91e8-498c-a84c-6342f132f3b6',\n",
       " 'a9ce2884-50e0-4734-a1ef-678230096715',\n",
       " '5ab62ccd-f2f5-4367-899d-5fe319ad3f21',\n",
       " '46b77c1b-388f-466d-ba26-e19b844f9349',\n",
       " '3f0fc867-0719-4830-b4c5-06a991dadf20',\n",
       " '9fcde4d3-b080-476c-b114-09434b72cc9c',\n",
       " '6e97f115-935e-47cc-8cb2-f21a298e3c2b',\n",
       " '2a24a9c6-d26a-4e35-9fa2-57cd41d98922',\n",
       " '82998e59-a814-4933-921c-468e51077e35',\n",
       " '1cf6823e-8ccd-451d-9abc-c4ba3aee0474',\n",
       " '3c2a9c74-ea7e-41e8-87e4-ca8d1ea49e21',\n",
       " '56e20b93-2a5c-42a2-82d2-2ada5f1cc427',\n",
       " 'e9550c5f-8f97-40cf-a3e9-000d8e03638a',\n",
       " '4d936c50-d6d0-48e6-a831-952a12c5b524',\n",
       " 'ca202dd3-f24e-4a06-a834-301e819dcaec',\n",
       " '7849356b-49e2-4416-9e2d-5078410d2045',\n",
       " '0d3174f8-e629-4d2c-a53b-85d5fca18899',\n",
       " 'c0ed8884-4f98-425c-8d4b-1f1537add7b3',\n",
       " '10a714af-fb88-4af1-b33d-5213aa75e445',\n",
       " 'a66bcf7e-2d0a-4bdf-9a53-2a4a8c86d7ec',\n",
       " 'a0dbf31d-88cf-4831-a759-4acb8c1a2d28',\n",
       " '6b18b24c-fe0f-4c80-9cf0-d2622f2aa906',\n",
       " 'd9c65ec3-fdb1-456a-a64a-25b0033ee51b',\n",
       " 'e46f33b1-fdd7-4a73-a00e-9b6bedd40294',\n",
       " 'b3d4aae4-2c83-4e0b-8048-127448f4255c',\n",
       " 'aa812016-3cc0-4141-929a-9e9987bd1304',\n",
       " '6a61a531-780c-458f-a7b0-ececc14c88fb',\n",
       " 'dcb4e7bf-1a98-4e38-bcc9-7190e6df7042',\n",
       " 'b5684fc2-8182-441e-9f9b-feb44204f2df',\n",
       " 'd5b55162-f142-4892-8a41-84702b706273',\n",
       " 'ccafed91-bbcb-4572-b2cb-ca7f40dd3f6e',\n",
       " 'dc94df05-9668-4a05-a0a1-8ff5b9a10191',\n",
       " 'd883642c-4d78-4755-b055-54f6e1e450ad',\n",
       " '98afa3f5-8247-4a38-9b9c-f9b0ebdd0377',\n",
       " 'd6911ef7-67e2-4223-832d-cf32a0f0520c',\n",
       " 'cf3ad47a-0cf1-473c-aa8e-0e202c475015',\n",
       " 'b8ae9beb-7061-40ce-a7bd-f02ba7f1c510',\n",
       " '5a2ff61e-a25a-4e0d-bd76-b363a4e9424c',\n",
       " '5810fe84-2c5f-4ae2-a3e3-ed32bb28fbcf',\n",
       " '140c6aca-0cad-4735-9afe-da1fbd6a23c6',\n",
       " '5635432a-b056-44da-a972-b490fd46dbf5',\n",
       " 'ab5fc380-855c-41d4-8785-4cd52d0d1c82',\n",
       " 'f6bc48d3-8521-4d4b-8b21-116186d1eb2e',\n",
       " '18712efb-0295-45d8-8220-f6021ffccf1f',\n",
       " '4e258af0-3631-41ca-9363-9c7106eddf61',\n",
       " '773aa4b1-88ef-40db-b114-b8ea0aea1938',\n",
       " '2d3e0dcb-5879-4231-8844-0178c0fc2119',\n",
       " '6905a6c2-31e7-44fd-bca4-70bae98f7ad6',\n",
       " '40514bab-2831-4c36-ac74-b82aeee01b2c',\n",
       " '8e97f349-4a21-4833-9cf2-a1900bd7d3f1',\n",
       " '701c578d-fb84-411b-879a-742f6f674784',\n",
       " '53d4ff4c-abc5-462f-a341-cd7777846295',\n",
       " 'bcce85ee-f4be-4a7b-9174-b32858cef5dd',\n",
       " '9d54cd3b-7987-4e76-a239-89ae94d46db7',\n",
       " 'ae1926ab-61d0-406a-86c7-0f8f2aaa80e0',\n",
       " '7291cd6d-ae5a-409d-ba91-e906378f3c8f',\n",
       " '9e64b842-ce5a-4e98-aa3b-0c36161795c8',\n",
       " '33235051-d923-4633-a2cd-4352ff44d1eb',\n",
       " '4ee6673c-092b-4de0-a1ef-cc1a6f8d31a8',\n",
       " '101a6043-a1dc-4ee3-90fb-50d60e34cea4',\n",
       " '376597c5-b441-4f92-8db6-acc3106e5a21',\n",
       " 'ea831f8c-64e8-48bb-94c6-5a1926bb6342',\n",
       " 'a61cb082-f98c-40bc-8d5d-196d5139eb5e',\n",
       " '886cda62-3642-461c-9a1a-fadd1ac025cf',\n",
       " '74cfa8d0-d1a9-4e84-b4d3-8862efc1fc95',\n",
       " 'b8ae406d-2875-4824-8d0c-483ae180f06f',\n",
       " 'e1ac91f5-f355-4f61-b744-eebe88519428',\n",
       " 'da6fa2e8-582f-4914-bad2-6458212f8be2',\n",
       " 'c36f60a2-9aaa-468c-95cc-157187ddfcc0',\n",
       " 'c4519723-368c-4cf0-a52f-197ffaa6d00b',\n",
       " '1fa0149d-aca4-43bc-9bf0-4879a32efd25',\n",
       " '6a2dde5f-1fc1-4c13-af6e-1ba288ddc48a',\n",
       " '42e402b1-252f-45fe-8eaa-8d77a1cc54c6',\n",
       " 'c20a01e5-9d33-4a5f-b082-067f34a1d2e4',\n",
       " '9555ea2d-3fd8-44bc-8139-bf651699241a',\n",
       " 'bb94f61f-c442-4993-8b8d-05063204e8e3',\n",
       " 'd1d1aa5e-be00-4aca-992b-6d7a5fc36f2d',\n",
       " '5c11313f-9316-4a2c-941c-813e39a359a3',\n",
       " '97e730ad-48ad-4290-ae73-f757877d071e',\n",
       " 'eeaf7bda-ca40-4b5d-866f-27841e799962',\n",
       " 'a2bde1c8-6fe9-4729-a3e1-11e829dea637',\n",
       " 'f1c82bd5-9525-4105-9f4a-514e4507e157',\n",
       " 'ab42a56d-45fd-4d3c-921a-2d19975a74a6',\n",
       " 'e2caa28b-3234-4175-bb79-0fcae1fc8004',\n",
       " 'af7bd213-557e-4414-97b5-029814ab9a7d',\n",
       " '65aeab04-1416-4f88-ac2f-43e9df612176',\n",
       " '35caa1cf-f4ce-4599-8cd2-7d63b965f27b',\n",
       " 'f550bd77-78be-4154-90af-695535d36674',\n",
       " '44d1dd03-944b-4273-896d-c43a71effb6a',\n",
       " 'cfcac26f-381f-40c9-92fe-b281c24ef619',\n",
       " '605692bd-6de0-4ceb-a8f9-836a76f0ff79',\n",
       " '9bdbf599-fbcd-4b4a-acac-a4957e854bc2',\n",
       " '5eb8c1b2-e3b4-4d5b-99ee-d89c7467d1e8',\n",
       " '5254204b-62fd-42bc-a3d3-e76ece49f979',\n",
       " '81e9b5d3-bbea-43e0-acc1-a711dc5b4f5d',\n",
       " 'f4a7553a-1717-4aa6-9d77-7dcf62f5cc32',\n",
       " '9692cae3-b593-4235-bb6a-90785f0d6c6f',\n",
       " '31d74547-bb20-4d5d-82c4-fd8eaf7db7fa',\n",
       " 'a02c9286-982a-4e0b-abdd-ad078807c947',\n",
       " 'f73477ae-a0fd-4683-b5a9-22247f2fc774',\n",
       " 'da00e392-93d2-451c-ab8b-03f0f6e59b8b',\n",
       " 'b25b03b0-2723-41a0-b57a-1b14912b599f',\n",
       " '4ec09856-3591-4460-9b63-2042b148889c',\n",
       " '1cba8deb-6323-4e00-a6e0-80e4ba4a573e',\n",
       " '6b11d764-21c3-424a-b0bc-023a4ecd3c09',\n",
       " 'e6cca0a1-b0b2-4c15-9178-057219f90f6c',\n",
       " 'a1f41213-51f3-4f6d-b764-b77a078864db',\n",
       " '2326a2ba-045a-462a-9ce8-c0623aa7fd81',\n",
       " 'eb417864-1ac2-4ee2-aed1-76542737781a',\n",
       " 'b4351df1-77ac-4ff0-b3fe-65b50062ff36',\n",
       " 'cebfe23f-3e3b-4fef-a198-3eb13d554da5',\n",
       " '4d7bd6fa-1dd8-4d01-9ad7-b12b6a67f895',\n",
       " '78c7f079-0bd2-4965-a48b-5dd28e66a2b1',\n",
       " '7abaa71d-fc78-4079-915d-7c2e126bedf4',\n",
       " 'a3a5820c-0ebb-4fd4-a760-0be3b6d4981f',\n",
       " '419f3cb2-28b8-468d-b95e-f1934c41bd38',\n",
       " '3eeda009-4d26-4b33-ad7f-b5dc3324bddd',\n",
       " '64fb6377-94d2-4f0e-947e-ed07cd0bf161',\n",
       " 'f95b35d9-1cf3-47e9-8dbe-3a486b14c0f8',\n",
       " '0061584d-9f5f-4330-b108-ebd3f42a9cc9',\n",
       " '31ac0030-78bc-421a-9c3d-e03186bda47e',\n",
       " '275ed31d-32b5-40a3-9531-313eee27397b',\n",
       " 'f4d6fa6e-5b88-4ce2-8db1-12aead0b12db',\n",
       " 'f50545ab-d8ca-4812-b9f2-e6b6a8797e0d',\n",
       " 'f7f21d8b-f171-4198-843d-3da0c2b58c92',\n",
       " '76e9e99b-e5ab-4e86-97b3-c795cb400c86',\n",
       " 'e2ea2be9-2adc-496d-b5f4-474c2b757559',\n",
       " 'e579e9b9-84ed-4523-9d32-ac8d0a4a731a',\n",
       " '99f5aa3f-a8d1-4f9a-8960-00aaea04f825',\n",
       " '861e40f8-8986-4f7f-a702-92729544682c',\n",
       " 'e2a6e64f-ba86-4e05-9d68-83cc08b83115',\n",
       " 'c021dbc6-3480-4285-a4aa-22de80b57afe',\n",
       " '4bd522e4-422b-4f57-bcc5-d0bb50936d12',\n",
       " 'c688b3cf-3dd0-42eb-8c4f-3031d3233682',\n",
       " '3e23193d-d8ca-4f2e-b089-e57791363a14',\n",
       " '0868051f-1e39-4d1f-b707-b43786857706',\n",
       " '8cfe1fc1-361d-4ba5-8c90-cca61e3ff426',\n",
       " 'affbd5e0-9b0e-4d53-9505-008e12a71cc5',\n",
       " 'c9d50bf3-4ce9-4032-a9ea-2cfc987ef1c8',\n",
       " 'b298b72c-d22c-4f1b-a66e-8bd4372f40e8',\n",
       " '3aeeed0a-5c67-4c1d-9c0a-0f9dc3f89728',\n",
       " '9cd48848-4374-457a-adb9-191cb2d1f87c',\n",
       " '4a36f06b-6a8c-40a4-a92b-7b2bbeaa0edc',\n",
       " '6ffb84d2-ac9b-4e53-a91f-080be03adf96',\n",
       " 'f6339214-ecdc-4773-b05b-c68b9a155061',\n",
       " 'b0cfcb67-c321-4641-adf2-bc9e6a7e0165',\n",
       " 'f139a277-fd32-4a61-a5eb-cded8e9906ee',\n",
       " '81ddd587-7925-45c0-a3d5-2c3d192ed790',\n",
       " 'dbe85663-de64-4352-8990-fdb0fdc5e50b',\n",
       " 'e78a4578-ab58-4dcc-ab47-6372c7fe58ba',\n",
       " 'b216f09f-76c3-475e-a56b-7781c4655dfc',\n",
       " '5ee6e203-0b3d-4849-af8a-d9fa860bd29e',\n",
       " 'e280c0e0-776c-4362-96b1-720a453999a5',\n",
       " '104686e3-5140-449e-be48-010456e7968f',\n",
       " '0d93593c-0c2f-4aee-836f-e7c765f945a4',\n",
       " 'dad6f18a-a8d8-4335-8520-124fddf97d2e',\n",
       " '374783d2-3537-4092-8a44-40360e8c76ca',\n",
       " '46052cfb-39c8-449b-a97e-96ac2836b711',\n",
       " 'd54f3a21-3902-4569-99cc-d3839dbbd1dc',\n",
       " 'eb79ab00-23f6-4a50-952a-e1b8780ea76c',\n",
       " '97393499-1d34-473a-9124-46733811ffa1',\n",
       " '257f197e-76ff-4a7d-a7fe-ba3054e47d72',\n",
       " 'a47f92bb-111b-49b3-9393-a32f1c7b9823',\n",
       " '0cd2e6a1-18ba-40ad-bd9c-4b8f077288d8',\n",
       " 'a3b36ed9-a6e5-4845-8bdb-1c13024b4944',\n",
       " 'bd62ef40-c7a5-4f00-9493-bdca156c4540',\n",
       " 'e8c863ab-776a-4ae7-89b0-df04732e7a61',\n",
       " 'ab166479-2845-44f7-8ac8-e2985f6d449d',\n",
       " 'a83c1e17-15dc-4fff-97ee-f16ea8fa5b40',\n",
       " 'bcd80e5f-374f-4576-bbf2-223b07f665a4',\n",
       " '85753a53-e0a9-4820-a066-12eb74e89c62',\n",
       " '2c9a8b91-2c28-4fdf-b0c0-cc6d2f9e76af',\n",
       " '552aecf3-c421-408e-b85d-3bf8822b77e4',\n",
       " '9ad8b945-f442-454b-81d0-50f69ca12528',\n",
       " '3d701bed-ab08-480f-b810-18c8ae5edace',\n",
       " '9e1cedfc-99d4-4f03-9255-47aa55423b52',\n",
       " '56849efa-f4bb-44dd-b78a-b3fa4dcc6211',\n",
       " '4533497b-b347-48b2-8284-11e3b7eb1ccd',\n",
       " 'eac142c4-2247-4844-adb5-c6e3b6407293',\n",
       " 'd65e1c88-4b7a-4eea-ac32-b83ff8f0afb1',\n",
       " 'd33013e5-bd0c-48bf-9485-674614d08378',\n",
       " 'ff3ae47e-49da-4970-aec8-4bf972e8eb75',\n",
       " '72d28e5c-0980-4bae-ac99-4468a35e2dea',\n",
       " '2a84d076-eeea-48d4-af96-6c867a24b950',\n",
       " 'b19c0062-1996-4188-bc6c-35f8fc44be34',\n",
       " 'c12f89a8-8196-482b-bc82-d986f84e248a',\n",
       " '65e45e00-acb5-4e41-96c1-ba3a70c76009',\n",
       " 'e53e5fb9-a32a-498f-bc1d-6ce484a41ffa',\n",
       " '179a00e8-a3ac-4357-9764-8062362179e7',\n",
       " '24af4aff-dd77-4846-b3a2-103eee559103',\n",
       " '0b9040a5-dc27-4169-b973-bd808103c26a',\n",
       " '9a257d91-d6ad-4263-bd25-a02becc5b322',\n",
       " 'e3f0c304-584a-4027-b950-3d64180741de',\n",
       " '6a659794-068c-48b0-bcaa-f1c7f1e08bcd',\n",
       " 'df6cb391-1ac3-499c-b62c-3791b27dff38',\n",
       " '4d27e4be-d2c2-481a-95c0-f8b4d497b1d9',\n",
       " '47122ab5-4307-48e4-8d26-7b1f23a1f275',\n",
       " '5ba95293-dd34-48c8-aaf5-51417341493a',\n",
       " 'b8f3b461-3b0f-47f4-8430-0098b77e5123',\n",
       " '8f418c08-d642-4adc-b9b8-182f69635cca',\n",
       " 'e9566947-0dae-4abd-893e-d6c25b676360',\n",
       " '9bf3f82e-fd10-49bd-ac7c-aacd965d5f4f',\n",
       " 'a8383fd2-a567-4c4d-8e02-c4aae784541e',\n",
       " 'e7e80547-dca1-4c0a-a970-efbfaf244d63',\n",
       " 'a701d721-31f7-4fef-abab-2491f2d2539c',\n",
       " '8899c8b9-0ebf-4f4c-b706-e3c763c582eb',\n",
       " '9d374c5f-056c-489f-a63c-a98977e87e71',\n",
       " 'b72bc712-ac9f-41ff-8a53-3fb30c04566e',\n",
       " '66c07ba7-08ee-46cd-80d6-320e7e87038f',\n",
       " '963849c4-5ea3-40d9-8caf-c46c91b10471',\n",
       " '117b9169-b92a-4338-a0d9-2a0096c0ed41',\n",
       " '769041f3-e6d5-4c74-9906-d1cbfe17a7d6',\n",
       " 'de964168-3662-4359-832b-9f5f34c1393e',\n",
       " 'bb77fc30-3a5e-43cc-b889-8decb3886403',\n",
       " '6d1226b1-a7d0-49a1-a0ef-ccd8696bdc5d',\n",
       " '89ba8c63-5810-4d20-aafa-931505125333',\n",
       " '31fb2999-9375-4041-87b8-f31d733d35f1',\n",
       " '9accb244-d384-4d31-b46d-c97b29d1a045',\n",
       " 'c52a64cd-cc42-4b50-8b61-4da2d2e69b88',\n",
       " 'c48744e8-c9ee-43b4-b0d1-06b59d7e4675',\n",
       " '5f91ebe2-04f9-4ae4-97e7-ceee3a969af2',\n",
       " '2c92b082-1e7c-428c-8462-8f210d01542f',\n",
       " '84adc2dd-6167-4c8d-b93a-e79b7e6f64f9',\n",
       " '92e90e0d-ead4-4ba3-94ef-ec0903c9efb4',\n",
       " '31eb4c07-bbeb-4cff-8807-cc99ff6003b7',\n",
       " '9798aa3f-e9f1-4204-8c6d-90954be76e4f',\n",
       " 'f3676c03-d9cb-4a3e-a152-9e22dc107214',\n",
       " 'c0bc1c18-5de0-4ad4-b138-07e581f03208',\n",
       " '6fa54b8c-a91a-4929-af98-b85cae236930',\n",
       " 'f904f9d7-3c22-4659-8c90-f26d7ea4c79a',\n",
       " 'cf321de3-4001-4dbd-9d6c-f537f1d6b0fb',\n",
       " '7d526fae-13a0-4c00-b939-93828e29135b',\n",
       " '47116633-522c-4c71-b3cc-be484ee34635',\n",
       " 'd56dedea-dd9b-41c0-90cf-86ed59a10b0b',\n",
       " '12a26d98-93f9-44db-b125-6111c651ca9e',\n",
       " 'caeaff0d-f4a8-4a51-8670-601e8a5e15ea',\n",
       " '62f3d7f4-09b5-4b6e-8af1-7a76033ba014',\n",
       " '5e170e4a-1bd2-4cf5-9ea2-3a76c3a0f0e2',\n",
       " 'adb5907c-33d8-4909-aa9a-c4d96a745413',\n",
       " 'e7aeb0b0-789d-4ae6-b363-1f4ef37aee59',\n",
       " '32e8cd73-a51c-4a21-84e3-f300ac1bfecc',\n",
       " '5955b4ab-e530-4f3d-9b75-ba408260298d',\n",
       " '61a8b0dc-2658-4db8-b179-d9db5ebedc88',\n",
       " '552510f4-ca03-4d89-8480-cfedca40203a',\n",
       " '313d3fd2-0fc3-4da8-8293-2ccc38302d66',\n",
       " 'b573a0cb-bcab-4422-b431-baf886d65deb',\n",
       " '7de6164a-9a06-4201-aac8-fd0e04903e16',\n",
       " '63a77357-e0f0-4032-bbdf-a50a2481bf37',\n",
       " 'cbaf6711-67dc-45ce-92bf-dd851190fddd',\n",
       " 'a093d2fe-2606-4d62-ac9f-25b5c6b16d13',\n",
       " '9090d1b1-41c8-48b7-8e66-3899dd0298b8',\n",
       " '4e9c75a0-73c4-4389-b026-e9a143980b45',\n",
       " '1f143a96-5913-49a6-9ec9-2c6dc5fd46ed',\n",
       " 'f822f3e5-21f6-4c51-a7ec-2f359d28ae0c',\n",
       " 'a75d20a7-9e01-4a49-a15e-c6f1b3c4eb00',\n",
       " '6d775d77-cf97-4682-9aae-cb6d8ce1f70f',\n",
       " '51fdb4ec-ae63-449b-a7c6-7df0f0641e70',\n",
       " '24001b0a-7a42-48a8-8fa7-9a048b478024',\n",
       " 'c52bd490-3e0b-4245-affb-894d770e1a6b',\n",
       " '83bf583d-e8c0-4b29-abd7-967f322af652',\n",
       " '1fecd4f2-624c-458d-8d9d-fd6da4f380e4',\n",
       " 'cf46e22e-72c0-424a-98fa-bd982719eaf1',\n",
       " '4797b790-48e5-4581-a7e4-6ddc7a330165',\n",
       " '6b62e4ca-cece-4513-96ce-4d5343c5e02c',\n",
       " 'e1262694-16a7-4818-b456-f6ec845d5985',\n",
       " '9c736a12-4eaf-405c-b919-3cbc1c46309a',\n",
       " '696702dd-b4fb-48f3-a6c6-921f5dc6896d',\n",
       " 'afff5db0-d632-41db-8319-ef2c46d5259e',\n",
       " 'e4498ea2-ea1f-4693-8caa-287bab3b1814',\n",
       " '074a2064-9ee6-4bea-8b9f-097fd4941787',\n",
       " '33d3b41c-2536-4b48-9bea-134b8af3ecb4',\n",
       " '32711296-cfaa-43a1-bfe2-0aa9d5c02bb4']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vector_store.add_documents(documents=documents)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8f78a32",
   "metadata": {},
   "source": [
    "## 二、检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "aae0375c",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_input = input(\"请输入:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "02348d93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化一个检索器\n",
    "retriever = vector_store.as_retriever(\n",
    "    search_kwargs={\"k\": 2}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "51e688ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "similar_docs = retriever.invoke(user_input)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcc47b0e",
   "metadata": {},
   "source": [
    "## 三、增强"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "70bc1e58",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "48ce94de",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = \"\"\"\n",
    "你是一个民法典问答机器人。\n",
    "你的任务是根据下述给定的已知信息回答用户问题。\n",
    "确保你回答的内容完全依赖于下面给出的已知信息，不能编造答案。\n",
    "如何下面的已知信息不足以回复用户的问题，请你直接回复'我无法回答你的问题。'\n",
    "\n",
    "已知信息：\n",
    "{info}\n",
    "\n",
    "用户提问：\n",
    "{question}\n",
    "\n",
    "自动检查语法错误。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1c4b14f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = PromptTemplate.from_template(prompt_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "49fc8024",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = template.format(info=similar_docs, question=user_input)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aa85c46",
   "metadata": {},
   "source": [
    "## 四、生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bd0f190",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.chat_models.tongyi import ChatTongyi\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "426476e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatTongyi(\n",
    "    model=\"qwen-max\",\n",
    "    api_key=\"sk-da90821cf9174fbeb854011015c67aad\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e8c5b284",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'根据您提供的已知信息，关于“宣告死亡”的相关规定如下：\\n\\n- **第四十六条** 规定，在以下情况下，利害关系人可以向人民法院申请宣告自然人死亡：\\n  - 下落不明满四年；\\n  - 因意外事件下落不明满二年。\\n  - 如果因意外事件下落不明，并且有关机关证明该自然人不可能生存的情况下，申请宣告死亡则不受上述两年时间限制。\\n\\n- **第四十七条** 指出，如果对于同一自然人，有的利害关系人申请宣告死亡而有的申请宣告失踪，只要符合宣告死亡的条件，人民法院应当作出宣告死亡的判决。\\n\\n- **第四十八条** 进一步说明了被宣告死亡的人其死亡日期如何确定：如果是法院作出宣告死亡判决，则以判决之日为死亡日期；若是因为意外事件导致下落不明后被宣告死亡，则意外事件发生之日视为死亡日期。\\n\\n这些条款共同构成了《中华人民共和国民法典》中关于宣告死亡的基本框架。如果您有更具体的问题或需要进一步的信息，请告诉我！'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.invoke(prompt).content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ce959b4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "learn_langchain",
   "language": "python",
   "name": "learn_langchain"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
